﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace GameBanChim_Version1
{
    public class StateMainGame : GameState
    {
        int _gameLevel;
        Background _backGroundGame;
        List<Bird> _listOfBird;
        LevelIntro _levelIntro;

        //Số lượng chim trong 1 game
        int _birdCount = 30;
        //Con chim thứ bao nhiêu dc cho bay
        int _iBirdCount = 0;
        //thời gian thả 1 con chim
        int _nTimeReleaseBird = 30;
        int _iTimeReleaseBird = 0;
        // kiem tra xong man game do chua
        bool _finish = false;
        int _nWaitToNextState = 100;
        int _iWaitToNextState = 0;

        int _nSlowDelayTime = 200;
        int _iSlowDelayTime = 0;

        int _nSleepDelayTime = 200;
        int _iSleepDelayTime = 0;

        SoundEffect _songGame;
        SoundEffectInstance _instance;

        public StateMainGame(int gameLevel)
        {
            _gameLevel = gameLevel;
        }

        public override void InnitState(MyGame owner)
        {
            base.InnitState(owner);

            if (_gameLevel == 1)
                GlobalVariables.GamePlayer.ResetAllMark();
            GlobalVariables.GamePlayer.ResetMark();
            GlobalVariables.GamePlayer.getLevelCurrentGame(_gameLevel);
            switch (_gameLevel)
            {
                case 1:
                    {
                        _backGroundGame = (Background)MyGameOwner.BackGroundManager.CreateObject(1);
                        break;
                    }
                case 2:
                    {
                        _backGroundGame = (Background)MyGameOwner.BackGroundManager.CreateObject(2);
                        break;
                    }
                case 3:
                    {
                        _backGroundGame = (Background)MyGameOwner.BackGroundManager.CreateObject(3);
                        break;
                    }
            }

            _listOfBird = new List<Bird>();
            Random _ran = new Random();
            for (int i = 0; i < _birdCount - 1; i++)
            {
                _listOfBird.Add((Bird)MyGameOwner.BirdManager.CreateObject(_ran.Next(0, 2)));
                _listOfBird[i].ICurrentLevel = _gameLevel - 1; //Lấy level cho con chim
                _listOfBird[i].X = 800; // Clone ra xong gan lai cai toa do X cho nó
                _listOfBird[i].Y = _ran.Next(0, 400);
            }
            _listOfBird.Add((Bird)MyGameOwner.BirdManager.CreateObject(0));
            _listOfBird[_birdCount - 1].ICurrentLevel = _gameLevel - 1;
            _listOfBird[_birdCount - 1].X = 800;
            _listOfBird[_birdCount - 1].Y = _ran.Next(0, 400);
            // intro game:
            _levelIntro = new LevelIntro(_gameLevel, MyGameOwner.Content);

            _songGame = MyGameOwner.Content.Load<SoundEffect>("Sound/GameSong");
            _instance = _songGame.CreateInstance();
            _instance.IsLooped = true;
            _instance.Play();
        }

        public override void Update(GameTime gameTime)
        {
            _backGroundGame.Update(gameTime);
            if (_finish)
            {
                if (_iWaitToNextState == _nWaitToNextState)
                {
                    _instance.Stop();
                    _instance.Dispose();
                    GlobalVariables.GamePlayer.MarkStore += GlobalVariables.GamePlayer.Mark;
                    MyGameOwner.MyGameState = new StateResult();
                    GlobalVariables.GamePlayer.Level = _gameLevel;
                    MyGameOwner.MyGameState.InnitState(MyGameOwner);
                }
                else
                {
                    _iWaitToNextState++;
                }
            }

            if (!_levelIntro.Finish)
            {
                _levelIntro.Update(gameTime);
                return;
            }

            if (_iBirdCount < _birdCount)
            {
                if (_iTimeReleaseBird == _nTimeReleaseBird)
                {
                    _iTimeReleaseBird = 0;
                    _listOfBird[_iBirdCount].BirdState = 1; //cho nó bay
                    _iBirdCount++;
                }
                else
                {
                    _iTimeReleaseBird++;
                }
            }
            else
            {
                if (!_listOfBird[_iBirdCount - 1].IsVisible())
                    _finish = true;
            }
            if (!_levelIntro.Finish)
            {
                _levelIntro.Update(gameTime);
                return;
            }
            for (int i = 0; i < _iBirdCount; i++)
            {
                _listOfBird[i].Update(gameTime);
            }
            GlobalVariables.GamePlayer.Update(gameTime);

            //Kiem tra su dung special skill
            if (GlobalVariables.GamePlayer.SkillSpecial.IsUse && !GlobalVariables.GamePlayer.UseSkillSpecial)
            {
                for (int i = 0; i < _iBirdCount; i++)
                {
                    if (_listOfBird[i].IsVisible())
                    {
                        GlobalVariables.GamePlayer.Mark += _listOfBird[i].Mark; //ghi điểm cho người chơi
                        if (_listOfBird[i].Mark == 5)
                            GlobalVariables.GamePlayer.LargeBirdCount++;
                        else
                            GlobalVariables.GamePlayer.NormalBirdCount++;
                        _listOfBird[i].BirdState = 2;
                        _listOfBird[i].Isprite = 1;
                    }
                }
                GlobalVariables.GamePlayer.UseSkillSpecial = true;
            }
            //Kiem tra su dung slow skil
            if (GlobalVariables.GamePlayer.SkillSlow.IsUse && !GlobalVariables.GamePlayer.UseSkillSlow)
            {
                if (_iSlowDelayTime < _nSlowDelayTime)
                {
                    for (int i = 0; i < _iBirdCount; i++)
                    {
                        if (_listOfBird[i].IsVisible())
                        {
                            _listOfBird[i]._flySlowly = true;
                        }
                    }
                    _iSlowDelayTime++;
                }
                else
                    GlobalVariables.GamePlayer.UseSkillSlow = true;
            }
            //Kiem tra su dung sleep skil
            if (GlobalVariables.GamePlayer.SkillSleep.IsUse && !GlobalVariables.GamePlayer.UseSkillSleep)
            {
                if (_iSleepDelayTime < _nSleepDelayTime)
                {
                    for (int i = 0; i < _iBirdCount; i++)
                    {
                        if (_listOfBird[i].IsVisible())
                        {
                            _listOfBird[i]._notFlying = true;
                        }
                    }
                    _iSleepDelayTime++;
                }
                else
                    GlobalVariables.GamePlayer.UseSkillSleep = true;
            }
        }

        public override void Draw(GameTime gameTime, SpriteBatch sb)
        {
            _backGroundGame.Draw(gameTime, sb);
            if (!_levelIntro.Finish)
            {
                _levelIntro.Draw(gameTime, sb);
                return;
            }
            for (int i = 0; i < _iBirdCount; i++)
            {
                _listOfBird[i].Draw(gameTime, sb);
            }
            GlobalVariables.GamePlayer.Draw(gameTime, sb);
        }
    }
}
